package com.maltys.dynamicdatasource.common.dynamic;

import com.alibaba.druid.pool.DruidDataSource;
import com.baomidou.mybatisplus.extension.spring.MybatisSqlSessionFactoryBean;
import com.maltys.dynamicdatasource.common.enums.DataSourceEnum;
import lombok.extern.slf4j.Slf4j;
import org.apache.ibatis.session.SqlSessionFactory;
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
import org.springframework.boot.autoconfigure.jdbc.DataSourceAutoConfiguration;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.boot.jdbc.DataSourceBuilder;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Primary;
import org.springframework.core.io.support.PathMatchingResourcePatternResolver;
import org.springframework.jdbc.datasource.DataSourceTransactionManager;
import org.springframework.transaction.PlatformTransactionManager;

import javax.sql.DataSource;
import java.util.HashMap;
import java.util.Map;

@Configuration
@EnableAutoConfiguration(exclude = {DataSourceAutoConfiguration.class})
@Slf4j
public class DynamicDataSourceConfiguration {

    @Bean("prime")
    @ConfigurationProperties(prefix = "spring.datasource.db0")
    DataSource dataSource0() {return DataSourceBuilder.create().type(DruidDataSource.class).build();}

    @Bean("active")
    @ConfigurationProperties(prefix = "spring.datasource.db1")
    DataSource dataSource1() {return DataSourceBuilder.create().type(DruidDataSource.class).build();}

    @Bean("backup")
    @ConfigurationProperties(prefix = "spring.datasource.db2")
    DataSource dataSource2() {return DataSourceBuilder.create().type(DruidDataSource.class).build();}

    @Primary
    @Bean
    public DataSource dynamicDataSource() {
        Map<Object, Object> dataSourceMap = new HashMap<>(4);
        dataSourceMap.put(DataSourceEnum.PRIME.getKey(), dataSource0());
        dataSourceMap.put(DataSourceEnum.ACTIVE.getKey(), dataSource1());
        dataSourceMap.put(DataSourceEnum.BACKUP.getKey(), dataSource2());

        DynamicDataSource dynamicDataSource = new DynamicDataSource();
        dynamicDataSource.setTargetDataSources(dataSourceMap);
        dynamicDataSource.setDefaultTargetDataSource(dataSource0());
        log.info("dynamicDataSource: loaded success");
        return dynamicDataSource;
    }

    @Bean(name = "transactionManager")
    public PlatformTransactionManager transactionManager() {
        return new DataSourceTransactionManager(dynamicDataSource());
    }

    @Bean(name = "sqlSessionFactory")
    @Primary
    public SqlSessionFactory sqlSessionFactory() throws Exception {
        final MybatisSqlSessionFactoryBean sessionFactory = new MybatisSqlSessionFactoryBean();
        sessionFactory.setDataSource(dynamicDataSource());
        sessionFactory.setMapperLocations(new PathMatchingResourcePatternResolver().getResources("classpath*:/mapper" +
                "/*.xml"));
        return sessionFactory.getObject();
    }
}
